<?php 
class LoginAction extends Action {
    //管理员登陆界面
    public function index() {
    	if ( service('Passport')->isLoggedAdmin() ) {
			$this->redirect( 'Admin://Index/index' );
		}
        $this->display('login');
    }
    
    public function logout(){
    	service('Passport')->logoutAdmin();
    	$this->redirect( 'Admin://Login/index' );
    }
    
	/* 管理员登陆 */
	public function doLogin() {
		// 检查验证码
		$this->assign('email', $_POST['email']);
		if ( md5($_POST['verify']) != $_SESSION['verify'] ) {
			$this->assign('error', '验证码错误！');
			$this->display('login');
			exit;
		}
		
		// 数据检查
		if ( empty($_POST['password']) ) {
			$this->assign('error', '密码不能为空！');
			$this->display('login');
			exit;
		}
		if ( isset($_POST['email']) && ! isValidEmail($_POST['email']) ) {
			$this->assign('error', 'email格式错误！');
			$this->display('login');
			exit;
		}
		
		// 检查帐号/密码
		$is_logged = false;
		if ( isset($_POST['email']) ) {
			$is_logged = service('Passport')->loginAdmin($_POST['email'], $_POST['password']);
		}else {
			$this->assign('error', '参数错误！');
			$this->display('login');
			exit;
		}

		if ($is_logged) {
			$this->redirect( 'Admin://Index/index' );
		}else {
			$this->assign('error', '密码错误！');
			$this->display('login');
			exit;
		}
	}
	
}